package iqq.ui.view.panel.login;

import iqq.ui.action.ActionManager;
import iqq.ui.action.LoginAction;
import iqq.ui.widget.StatusComboBox;
import iqq.util.LanguageLoader;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;

import javax.swing.Action;

import com.alee.extended.panel.BorderPanel;
import com.alee.extended.panel.CenterPanel;
import com.alee.extended.panel.FlowPanel;
import com.alee.extended.panel.GroupPanel;
import com.alee.laf.button.WebButton;
import com.alee.laf.checkbox.WebCheckBox;
import com.alee.laf.combobox.WebComboBox;
import com.alee.laf.label.WebLabel;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.text.WebPasswordField;

/**
 * @author ChenZhiHui
 * @create-time 2013-3-8
 */
public class CenterPl extends WebPanel {

	private WebLabel accountLbl;
	private WebLabel passwordLbl;

	private WebComboBox accoutCbx;
	private WebComboBox statusCbx;
	private WebPasswordField passwordFld;

	private WebCheckBox rePwdCkb;
	private WebButton loginBtn;
	private GroupPanel groupPl;

	private Action loginAction;

	public CenterPl() {
		initLayout();
		initComponent();
		initStatus();
		addComponent();
		initAction();
	}

	private void initAction() {
		loginAction = ActionManager.getAction(LoginAction.class);
		loginAction.putValue("account", accoutCbx);
		loginAction.putValue("password", passwordFld);
		loginAction.putValue("repassword", rePwdCkb);
		loginAction.putValue("status", statusCbx);
		loginBtn.setAction(loginAction);
	}

	@SuppressWarnings("unchecked")
	private void initStatus() {
		statusCbx.add(new WebLabel("ok"));
	}

	private void initLayout() {
		// this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		// this.setLayout(new FlowLayout(FlowLayout.LEFT));
	}

	private void addComponent() {
		add(groupPl);
	}

	private void initComponent() {
		// new component
		Insets insets = new Insets(10, 10, 10, 10);
		Dimension d = new Dimension(this.getWidth(), 30);
		accountLbl = new WebLabel(LanguageLoader.getString("account"));
		accountLbl.setMargin(insets);

		passwordLbl = new WebLabel(LanguageLoader.getString("password"));
		passwordLbl.setMargin(insets);

		accoutCbx = new WebComboBox();
		accoutCbx.setEditable(true);
		accoutCbx.setPreferredSize(new Dimension(d));

		passwordFld = new WebPasswordField();
		passwordFld.setPreferredSize(new Dimension(d));

		rePwdCkb = new WebCheckBox(LanguageLoader.getString("rememberPwd"));
		rePwdCkb.setMargin(insets);

		statusCbx = new StatusComboBox();

		loginBtn = new WebButton(LanguageLoader.getString("login"));
		loginBtn.setAlignmentX(Component.LEFT_ALIGNMENT);
		loginBtn.setPreferredSize(new Dimension(100, 30));

		// add to group panel
		FlowPanel p = new FlowPanel(rePwdCkb, statusCbx);
		p.setOpaque(false);
		p.setMargin(10, 0, 20, 0);
		groupPl = new GroupPanel(0, false, new BorderPanel(accountLbl),
				new BorderPanel(accoutCbx), new BorderPanel(passwordLbl),
				new BorderPanel(passwordFld), p, new CenterPanel(loginBtn));
		groupPl.setMargin(10, 25, 10, 25);

	}

}
